package com.hn658.base.message.notification.msg.dao.mongo.impl;

import com.hn658.base.message.notification.msg.dao.mongo.IMessageMongoDBDao;
import com.hn658.base.message.notification.msg.entity.MessageEO;
import com.hn658.framework.dataaccess.mongo.AbstractMongoDBDaoImpl;
import com.hn658.framework.dataaccess.pagination.GenericPage;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by baihai on 2015/11/24.
 */
@Repository
public class MessageMongoDBDaoImpl extends AbstractMongoDBDaoImpl<MessageEO> implements IMessageMongoDBDao{


    @Override
    public Long getCount(Map<String, Object> queryParam) {
        if (queryParam == null) {
            return null;
        }
        Query query = queryParamInit(queryParam);
        return this.mongoTemplate.count(query, clazz);
    }



    @Override
    public GenericPage<MessageEO> queryPage(Map<String, Object> queryParam, int pageSize,
                                       int startIndex, String orderBy, boolean isAsc) {
        if (queryParam == null || pageSize < 0 || startIndex < 0) {
            return null;
        }
        Long count = getCount(queryParam);
        Query query = queryParamInit(queryParam);
        logger.debug("======聊天消息Mongodb查询条件======："+query.toString());
        if(isAsc){
            query.with(new Sort(Sort.Direction.ASC, orderBy));
        }else{
            query.with(new Sort(Sort.Direction.DESC, orderBy));
        }
        query.skip(startIndex);
        if (pageSize > 0) {
            query.limit(pageSize);
        }
        List<MessageEO> list = this.mongoTemplate.find(query, clazz);
        
        return new GenericPage<MessageEO>(startIndex, count, pageSize, list);
    }

	@Override
	public List<MessageEO> queryList(Map<String, Object> queryParam) {
		if (queryParam == null) {
            return null;
        }
		Query query = queryParamInit(queryParam);
		return this.mongoTemplate.find(query, clazz);
	}



	private Query queryParamInit(Map<String, Object> param) {
        Map<String, Object> queryParam = new HashMap<String, Object>();
        queryParam.putAll(param);
        Query query = new Query();
        //判断是单聊还是群聊
		if (null!=queryParam.get("toUid")) {
			//如果是单聊添加聊天双方对象条件
			query.addCriteria(Criteria.where("fromUid").in(queryParam.get("uid"),queryParam.get("toUid")));
			query.addCriteria(Criteria.where("toUid").in(queryParam.get("uid"),queryParam.get("toUid")));
			queryParam.remove("uid");
			queryParam.remove("toUid");
		}
        for (String key : queryParam.keySet()) {
            Object value = queryParam.get(key);
            if (value != null) {
                query.addCriteria(Criteria.where(key).is(value));
            }
        }
        return query;
    }


    @Override
    public Long getMaxId() {
        Query query = new Query().with(new Sort(Sort.Direction.DESC, "_id"));
        MessageEO messageEO = this.mongoTemplate.findOne(query, clazz);
        return messageEO == null ? 0 : messageEO.getId();
    }




}
